package com.terry.gamehall.test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureTest3 {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) {

        //两任务组合
        //thenCombine :组个两个future,获取两个future的返回结果,并返回当前任务的返回值
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一"+Thread.currentThread().getId()+"开始");
            int i = 10 / 2;
            System.out.println("运行结果 " + i);
            System.out.println("任务一"+Thread.currentThread().getId()+"结束");
            return i;
        }, executor);


        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二"+Thread.currentThread().getId()+"开始");
            System.out.println("任务二"+Thread.currentThread().getId()+"结束");
            return "hello";
        }, executor);
//        future.runAfterBothAsync(future2,()->{
//            System.out.println("任务三"+Thread.currentThread().getId()+"开始");
//            System.out.println("任务三"+Thread.currentThread().getId()+"结束");
//        },executor);
//        future.thenAcceptBothAsync(future2,(f1,f2)->{
//            System.out.println();
//        });
        future.thenCombineAsync(future2,(f1,f2)->{
            System.out.println();
            return 0;
        });
        //thenAcceptBoth:组合两个future,获取两个future任务的返回结果,然后处理任务,没有返回值
        //runAfterBoth:组合两个future,不需要获取两个future任务的返回结果,只需两个future处理完任务后处理该任务

    }
}
